<?php

App::uses('AppHelper', 'View/Helper');

class BoniHelper extends AppHelper {
	
	public $helpers = array('Form', 'Html');
	
	public function autocomplete($id, $options) {
		$value = empty($options['value']) ? array('','') : $options['value'];
		if(!is_array($value)) {
			$value = array($value, $value);
		}
		
		if (empty($options['onselect'])) $options['onselect'] = 'void';
		
		$hidden = $this->Form->hidden($id, array('value' => $value[0], 'class'=>'autocomplete-value'));
		$text = $this->Form->text("{$id}_autocomplete", array( 
			'autocomplete'	=> 'off', 
			'class'			=> 'autocomplete-search',
			'ac-url'		=> $options['url'],
		    'ac-select'     => $options['onselect']
		));
		$label = '<span class="autocomplete-label" title="'.$value[1].'">'.$value[1].'</span>';
		
		$this->Html->script('autocomplete-1.0.0', array('block'=>'script'));
		$this->Html->css('autocomplete.css', 'stylesheet', array('block'=>'css'));
        
        $reset = '';
        if ($options['reset']) {
            $reset = '<a href="javascript:void(0)" class="reset">x</a>';
        }
		
		$html = '<div class="autocomplete-container" id="' . $id . '-autocomplete-container">'.
		             $hidden . $label . $text . $reset .
		        '</div>';
		
		return $html;
	}
    
    public function store_name($store) {
        $name = $store['name'];
        
        if (empty($name)) {
            $name = $store['Client']['name'];
            if (! empty($store['address'])) {
                $name .= ", " . $store['address'];
            }
            if (! empty($store['city'])) {
                $name .= ", " . $store['city'];
            }
        }
        
        return $name;
    }
	
}

?>